package uk.petesoft.remindme;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Extension of the JLable widget that allows the user to click on the 
 * widget, and gets the segment of the widget that was clicked in relation
 * to minutes/hours on an analogue clock. 
 * 
 * @author Pete Tinkler
 * 
 */

public class JClockFace extends JLabel {
	
	private static final long serialVersionUID = 1L;
	private double hourHand = 0;
	private double minuteHand = 0;
	
	/**
	 * @return the hourHandTheta
	 */
	public double getHourHand() {
		return hourHand;
	}

	/**
	 * @param hourHandTheta the hourHandTheta to set
	 */
	public void setHourHand(double hourHand) {
		this.hourHand = hourHand;
		this.hourHand *= 30;
		repaint();
	}

	/**
	 * @return the minuteHandTheta
	 */
	public double getMinuteHand() {
		return minuteHand;
	}

	/**
	 * @param minuteHandTheta the minuteHandTheta to set
	 */
	public void setMinuteHand(double minuteHand) {
		this.minuteHand = minuteHand;
		this.minuteHand *= 6;
		repaint();
	}

	public JClockFace(String lblText) {
		super(lblText);
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Graphics2D gHourHand = (Graphics2D) g.create();
		Graphics2D gMinuteHand = (Graphics2D) g.create();
		
		//rotated minute hand
		gMinuteHand.setColor(Color.RED);
		gMinuteHand.rotate(Math.toRadians(minuteHand), 
				  	(this.getWidth() / 2), 
				  		(this.getHeight() / 2));
		
		gMinuteHand.fillRect(((this.getWidth() / 2) - 2), 
						((this.getHeight() / 2) - 100), 5, 100);
		
		//rotated hour hand
		gHourHand.setColor(Color.BLACK);
		gHourHand.rotate(Math.toRadians(hourHand), 
			  	(this.getWidth() / 2), 
			  		(this.getHeight() / 2));
		
		gHourHand.fillRect(((this.getWidth() / 2) - 2), 
						((this.getHeight() / 2) - 80), 5, 80);
		
		//center circle
		g.setColor(Color.BLACK);
		g.fillOval(((this.getWidth() / 2) - 10), 
						((this.getHeight() / 2) - 10), 
							20, 20);
	}
		
	public int getSegment(MouseEvent event) {
		int segment;
		int buttonPressed = event.getButton();
		Dimension dimensions = this.getSize();
		Point center = new Point((dimensions.width / 2), (dimensions.height / 2));

		//get the rise/run of the click relative to the image center
		double run = (event.getX() - center.getX());
		double rise = (event.getY() - center.getY());

		//find theta
		double theta = Math.atan2(run, rise);				
		theta = Math.abs(Math.toDegrees(theta) - 180);
		
		if (buttonPressed == 1) {
			
			//set the catchment-area offset for hours (+/- 30 minutes)
			theta -= 15;
			if (theta < 0) {
				theta += 360;
			}
			
			//gives the value of the hour
			segment = (int)((theta / 30) + 1);
			this.setHourHand(segment);
		} else if (buttonPressed == 3) {
			
			//set the catchment-area offset for minutes (+/- 30 seconds)
			theta += 3;
			if (theta > 360) {
				theta -= 360;
			}
			
			//gives the value of the minute
			segment = (int)((theta) / 6);
			this.setMinuteHand(segment);
		} else {
			segment = 0;
		}	
		return segment;
	}
}
